package com.heima.homeservicemanagementsystem.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.homeservicemanagementsystem.entity.ServiceClassification;
import com.heima.homeservicemanagementsystem.entity.ServiceServicedetail;
import com.heima.homeservicemanagementsystem.entity.UserFavoritesService;
import com.heima.homeservicemanagementsystem.entity.dto.Option;
import com.heima.homeservicemanagementsystem.service.ServiceServicedetailService;
import com.heima.homeservicemanagementsystem.service.UserFavoritesServiceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.heima.homeservicemanagementsystem.module.PageParams;
import com.heima.homeservicemanagementsystem.module.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.heima.homeservicemanagementsystem.entity.Servicedetails;
import com.heima.homeservicemanagementsystem.service.ServicedetailsService;

import java.util.List;
import java.util.Map;

import static cn.dev33.satoken.SaManager.log;

/**
 * 服务详情;(serviceDetails)表控制层
 *
 * @author : http://www.chiner.pro
 * @date : 2025-4-12
 */
@Slf4j
@Tag(name = "服务详情对象功能接口")
@RestController
@RequestMapping("/servicedetails")
public class ServicedetailsController {

    @Resource
    private ServicedetailsService servicedetailsService;
    @Resource
    private UserFavoritesServiceService userFavoritesService;

    @Operation(summary = "服务详情子级下拉列表")
    @GetMapping("/serviceChild")
    public Result<List<Option>> roleOptionschildren() {
        //查询参数

        QueryWrapper<Servicedetails> lqw = new QueryWrapper<>();
        lqw.select("service_name AS label", "id AS value");
        List<Map<String, Object>> maps = servicedetailsService.listMaps(lqw);

        List<Option> optionals = BeanUtil.copyToList(maps, Option.class);
        return Result.success(optionals);
    }

    @Operation(summary = "分页查询")
    @PostMapping("/page")
    @SaIgnore
    public Result<Page<Servicedetails>> paginQuery(@RequestBody PageParams<Servicedetails> pageParams) {
        //查询参数
        Servicedetails params = pageParams.getParams();
        LambdaQueryWrapper<Servicedetails> lqw = new LambdaQueryWrapper<>();
        log.info("信息：{}", StringUtils.hasLength(params.getServiceName()));

        lqw.like(StringUtils.hasLength(params.getServiceName()), Servicedetails::getServiceName, params.getServiceName());


        lqw.like(params.getSpecification().size() > 0, Servicedetails::getSpecification, params.getSpecification());


//        lqw.eq(params.getServicecarousel() != null, Servicedetails::getServicecarousel, params.getServicecarousel());


//        lqw.eq(params.getServicedetails() != null, Servicedetails::getServicedetails, params.getServicedetails());


//        lqw.eq(params.getQuantitysold() != null, Servicedetails::getQuantitysold, params.getQuantitysold());

        return Result.success(servicedetailsService.page(pageParams.getPage(), lqw));
    }


    @Operation(summary = "服务详情所有搜索")
    @GetMapping("/list")
    public Result searchQuery() {
        return Result.success(servicedetailsService.list());
    }

    @SaIgnore
    @Operation(summary = "根据id查询相关的详情")
    @GetMapping("/getdetailById")
    public Result getdetailById(@RequestParam String id) {

        Servicedetails byId = servicedetailsService.getById(id);

        return Result.success(byId);
    }

    @Operation(summary = "有条件的搜索")
    @PostMapping("/tjSearch")
    public Result Query(@RequestBody Servicedetails servicedetails) {
        LambdaQueryWrapper<Servicedetails> lqw = new LambdaQueryWrapper<>();


        lqw.eq(servicedetails.getId() != null, Servicedetails::getId, servicedetails.getId());


        lqw.like(servicedetails.getServiceName() != null, Servicedetails::getServiceName, servicedetails.getServiceName());


//        lqw.eq(servicedetails.getSpecification()!=null,Servicedetails::getSpecification,servicedetails.getSpecification());
//
//
//        lqw.eq(servicedetails.getServicecarousel()!=null,Servicedetails::getServicecarousel,servicedetails.getServicecarousel());
//
//
//        lqw.eq(servicedetails.getServicedetails()!=null,Servicedetails::getServicedetails,servicedetails.getServicedetails());
//
//
//        lqw.eq(servicedetails.getQuantitysold()!=null,Servicedetails::getQuantitysold,servicedetails.getQuantitysold());


        return Result.success(servicedetailsService.list(lqw));
    }

    /**
     * 新增数据
     *
     * @param servicedetails 实例对象
     * @return 实例对象
     */
    @Operation(summary = "新增数据")
    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody Servicedetails servicedetails) {
        if (servicedetailsService.save(servicedetails)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 更新数据
     *
     * @param servicedetails 实例对象
     * @return 实例对象
     */
    @Operation(summary = "更新数据")
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody Servicedetails servicedetails) {
        if (servicedetailsService.updateById(servicedetails)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Operation(summary = "通过主键删除数据",
            parameters = {
                    @Parameter(name = "id", description = "主键", required = true, in = ParameterIn.PATH)
            })
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteById(@PathVariable String id) {

        if (servicedetailsService.removeById(id)) {

            return Result.success();
        }
        return Result.error();
    }

    @Operation(summary = "多id删除数据")
    @DeleteMapping("/delete")
    public Result<Boolean> deleteByIds(@RequestBody List<String> id) {
        log.info("前端信息：{}", id);
        if (servicedetailsService.removeByIds(id)) {
            //当删除之后需要删除关联表的相关信息
            ServiceServicedetailService serviceServicedetailService = SpringUtil.getBean(ServiceServicedetailService.class);
            QueryWrapper<ServiceServicedetail> detailId = new QueryWrapper<ServiceServicedetail>().eq("detailId", id.get(0));
            serviceServicedetailService.remove(detailId);
            QueryWrapper<UserFavoritesService> serviceId = new QueryWrapper<UserFavoritesService>().eq("service_id", id.get(0));
            userFavoritesService.remove(serviceId);
            return Result.success();
        }
        return Result.error();
    }
}